﻿// -----------------------------------------------------------------------
// <copyright file="Authenticate.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ManagerClinic.Modules
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Security.Cryptography;
    using System.IO;
    using ManagerClinic.Ultilities.Ultility;
    using DevExpress.XtraBars;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using System.Configuration;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class Authenticate
    {
        public static bool View { get; set; }
        public static bool Add { get; set; }
        public static bool Update { get; set; }
        public static bool Delete { get; set; }
        public static bool Print { get; set; }

        public static string UserName { get; set; }
        public static string MaBacSy { get; set; }

        public static string ChuoiPhongKhamVaCanLamSangString { get; set; }
        public static string ChuoiPhongKhamVaCanLamSang()
        {
            var lstCauHinhEntity = new CauHinhBusiness().GetCauHinhList();
            if (lstCauHinhEntity.Count <= 0) return ChuoiPhongKhamVaCanLamSangString = string.Empty;

            var cauHinhEntity = lstCauHinhEntity.Where(n => n.MaNguoiDung == UserName).FirstOrDefault();
            if (cauHinhEntity == null) return ChuoiPhongKhamVaCanLamSangString = string.Empty;

            var lstCauHinhChiTietEntity = new CauHinhChiTietBusiness().GetCauHinhChiTietList(cauHinhEntity.MaCauHinh);
            return ChuoiPhongKhamVaCanLamSangString = string.Join(",", lstCauHinhChiTietEntity.Select(n => n.MaPhongKham).ToArray());
        }

        public const string sView = "View";
        public const string sAdd = "Add";
        public const string sUpdate = "Update";
        public const string sDelete = "Delete";
        public const string sPrint = "Print";
             
        public static bool IsAuthenticateParent(Form frm)
        {
            if (View == true) return true;

            return false;
        }

        public static void IsAuthenticateForm(Form frm)
        {
            foreach (Control c in frm.Controls)
            {
                if (c is DevExpress.XtraEditors.SimpleButton)
                {
                    switch (c.Name)
                    {
                        case "btnThemMoi":
                            if (Add == false) c.Enabled = false;
                            break;
                        case "btnSua":
                            if (Update == false) c.Enabled = false;
                            break;
                        case "btnXoa":
                            if (Delete == false) c.Enabled = false;
                            break;
                        case "btnXemIn":
                            if (Print == false) c.Enabled = false;
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        public static bool IsAuthenticate(string authenticate)
        {
            bool isAuthenticate = false;

            switch (authenticate)
            {
                case sView:
                    isAuthenticate = (View == true) ? true : false;
                    break;
                case sAdd:
                    isAuthenticate = (Add == true) ? true : false;
                    break;
                case sUpdate:
                    isAuthenticate = (Update == true) ? true : false;
                    break;
                case sDelete:
                    isAuthenticate = (Delete == true) ? true : false;
                    break;
                case sPrint:
                    isAuthenticate = (Print == true) ? true : false;
                    break;
                default:
                    isAuthenticate = false;
                    break;
            }

            if (isAuthenticate == false)
            {
                return false;
            }

            return true;
        }

        const string sKeyEncrypt = "&%#@?,:*";

        public static string Encrypt(string strText)
        {
            byte[] byKey = new byte[20];
            byte[] dv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(sKeyEncrypt.Substring(0, 8));

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputArray = System.Text.Encoding.UTF8.GetBytes(strText);

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, dv), CryptoStreamMode.Write);

                cs.Write(inputArray, 0, inputArray.Length);
                cs.FlushFinalBlock();

                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                LogExceptionUltilities.LogException("Encrypt", ex.Message);
                throw ex;
            }

        }

        public static string Decrypt(string strText)
        {
            byte[] bKey = new byte[20];
            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            try
            {
                bKey = System.Text.Encoding.UTF8.GetBytes(sKeyEncrypt.Substring(0, 8));

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] inputByteArray = inputByteArray = Convert.FromBase64String(strText);

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(bKey, IV), CryptoStreamMode.Write);

                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;

                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                LogExceptionUltilities.LogException("Decrypt", ex.Message);
                throw ex;
            }

        }

    }
}
